package org.itboys.mobile.service.mysql.mobile.product;

import org.itboys.mobile.dto.lasted.SundayUploadDto;
import org.itboys.mobile.service.lasted.SundayMobileUploadService;
import org.itboys.commons.utils.tools.ListUtil;
import org.itboys.framework.resource.ResourceHolder;
import org.itboys.mobile.common.SundayCommonConstants;
import org.itboys.mobile.dto.SundayCommentDto;
import org.itboys.mobile.dto.product.SundayProductDto;
import org.itboys.mobile.entity.mongo.SundayMember;
import org.itboys.mobile.entity.mysql.product.SundayProduct;
import org.itboys.mobile.service.mongo.mobile.SundayMobileCommentService;
import org.itboys.mobile.service.mongo.mobile.collection.SundayMobileCollectionService;
import org.itboys.mobile.service.mongo.web.SundayMemberService;
import org.itboys.mongodb.service.BaseMySqlService;
import org.itboys.param.ParamUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 作者：jiangxiong
 * 日期：2017年5月8日
 * 联系方式 ：empty90911@126.com
 * 描述：圣代_商品_service_mobile
 */
@Service
public class SundayMobileProductService extends BaseMySqlService<SundayProduct> {
    @Autowired
    private SundayMemberService memberService;
    @Autowired
    private SundayMobileUploadService uploadService;
    @Autowired
    private SundayMobileCollectionService collectionService;
    @Autowired
    private ResourceHolder resourceHolder;
    @Autowired
    private SundayMobileCommentService commentService;

    /**
     * 分页获取商品
     * @param request
     * @param memberId
     * @param type 1商城商品。2积分商品，3采购商品
     */
    public List<SundayProductDto> selectProduct(HttpServletRequest request,Long memberId,Long villageId,Integer type){
            Map<String,Object> param = ParamUtil.packageMysqlPageParam(request,"name","categoryId");
            param.put("status",1);
            //1商城商品，2积分商品，3采购商品
            if(type!=1&&type!=2&&type!=3){
                param.put("type",0);
            }else{
                //采购商品也是商城商品
                if(type==3)type=1;
                param.put("type",type);
            }

            return packageDtoMuilt(super.select(param),memberId,villageId);
    }

    /**
     * 查询商品详情
     * @param memberId
     * @param productId
     * @param villageId
     */
    public SundayProductDto findOne(Long memberId,Long productId,Long villageId){
        return packageDtoSingle(super.find(productId),memberId,villageId);

    }
    /**
     * 查询商品详情(分享时使用)

     * @param productId

     */
    public SundayProduct share(Long productId){
        SundayProduct product =super.find(productId);
        //加载轮播图
        List<SundayUploadDto> rollImages = uploadService.selectNoPage(productId,SundayProduct.class.getSimpleName());
        product.setRollImages(rollImages);
        return product;

    }
    /** 组装DTO
     * 不同的用户类型。商品价格享受价格不同
     * 1，普通用户。零售价格
     * 2，门店。批发价格
     * 3，大客户。折扣价格
     *
     */
    public SundayProductDto packageDtoSingle(SundayProduct product, Long memberId,Long villageId){

        SundayProductDto dto = null;
        //商城商品根据用户区别价格价格
        product=getRealPrice3(null,product,memberId,null,villageId,false).get(0);
        //组装轮播图
        List<SundayUploadDto> rollImages = uploadService.selectNoPage(product.getId(),SundayProduct.class.getSimpleName());
        //商品详情URL
        String descUrl =resourceHolder.getStringValue("mobileRoot")+"/sunday/mobile/product/desc?id="+product.getId();
        //判断受否收藏
        Integer isCollection =collectionService.checkIsCollection(memberId,SundayCommonConstants.collection_type_product,product.getId());
        //加载默认评论
        logger.info("加载评论");
        List<SundayCommentDto> comments = commentService.getCommentByNumber(2,product.getId(),SundayProduct.class.getSimpleName());
        logger.info("加载评论-结束");
        //List<SundayCommentDto> comments =null;
        //加载推荐商品
        Map<String,Object> param = new HashMap<String,Object>();
        param.put("categoryId",product.getCategoryId());
        param.put("status",1);
        param.put("row_start",0);
        param.put("row_size",2);
        param.put("type",product.getType());
       // List<SundayProductDto> productDtos  =packageDtoMuilt(super.select(param),memberId);
        List<SundayProductDto> productDtos  =packageDtoMuilt(super.select(param),memberId,villageId);
        List<Map<String,Object>> reProducts = new ArrayList<Map<String,Object>>();
        for(SundayProductDto productDto:productDtos){
            Map<String,Object> reProduct = new HashMap<String,Object>();
            reProduct.put("id",productDto.getId());
            reProduct.put("name",productDto.getName());
            reProduct.put("viceName",productDto.getViceName());
            reProduct.put("image",productDto.getImage());
            reProduct.put("price",productDto.getPrice());
            reProduct.put("marketPrice",productDto.getMarketPrice());
            reProducts.add(reProduct);
        }
        dto=  new SundayProductDto(product.getId(), product.getCreateTime(), product.getCreateDate(),
                product.getName(),  product.getViceName(), product.getNumber(),  product.getStandard(),  product.getMarketPrice(), product.getPrice(),
                product.getType(), product.getImage(),
                product.getSaleNumber(),rollImages,
                isCollection,  descUrl);
        //分享地址
        dto.setShareUrl(resourceHolder.getStringValue("mobileRoot")+"/sunday/mobile/product/share?id="+product.getId());
        dto.setShareImage(product.getImage());
        dto.setShareTitle("花果山果蔬 产品分享");
        dto.setShareDesc(product.getName()+product.getViceName()+product.getStandard());

        //加载关联菜谱
       //ist<Map<String,Object>> cookBooks = productCookBookService.getCookBooksByProduct(product.getId());
       // org.itboys.mobile.dto.setCookBooks(cookBooks);

        return dto;
    }
    public List<SundayProductDto> packageDtoMuilt(List<SundayProduct> products,Long memberId,Long villageId){
        List<SundayProductDto> dtos = new ArrayList<SundayProductDto>();
        //获取商品真实价格
       // getRealPrice(products,memberId);
      //  products=getRealPrice2(products,memberId,null,villageId);
        products=getRealPrice3(products,null,memberId,null,villageId,true);
        for(SundayProduct product:products){
            //主图
            dtos.add(new SundayProductDto(product.getId(), product.getCreateTime(), product.getCreateDate(),
                    product.getName(),  product.getViceName(), product.getNumber(),  product.getStandard(),  product.getMarketPrice(), product.getPrice(),
                    product.getType(), product.getImage(),
                    product.getSaleNumber(),null,
                    null,  null));

        }
       return dtos;
    }

    /**
     *按照小区，获取商品的真实库存和价格。此价格为不打折价格
     * @param products
     * @param sundayProduct
     * @param memberId
     * @param member
     * @param villageId
     * @param isCheckStore 是否校验库存。将库存不满足要求商品，不与显示
     * @return
     */

    public List<SundayProduct> getRealPrice3(List<SundayProduct> products,SundayProduct sundayProduct,Long memberId,SundayMember member,Long villageId,boolean isCheckStore) {



        Map<String, Object> param = new HashMap<String, Object>();
        if(ListUtil.isNull(products)){
            products=new ArrayList<SundayProduct>();
        }
        if(sundayProduct!=null){
            products.add(sundayProduct);
        }
        if(ListUtil.isNull(products)){
            return products;
        }
        //获取商品类型。1商城，2积分，3采购（也是商城商品，但是可以用用户类型判断）
        Integer productType=products.get(0).getType();

        //最后返回的商品集合
        List<SundayProduct> returnProducts=new ArrayList<SundayProduct>();
        if (member == null) {
            member = memberService.getById(memberId);
        }



        //app的最小上架库存量
        Integer  minimumStore= 0;
       // List<SundayPermission> permissions = new ArrayList<SundayPermission>();
        //获取当前商品真实价格和库存。只对【用户版本生效】

        //设置商品的库存和价格
        for(SundayProduct product:products){
            product.setImage(resourceHolder.getStringValue("imgRoot")+product.getImage());

        }
        return returnProducts;
    }
}
